home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / gg / tcpbug.lha / tcpbug / ip / tcp_connect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  932 b   |  47 lines

  1. #include <stddef.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11.  
  12. #include "ip_misc.h"
  13.  
  14.  
  15. #define    Export
  16.  
  17.  
  18. /*
  19.  *  Open a TCP connection to REMOTE.  Bind the local port to
  20.  *  LOCAL, if set.
  21.  *  Returns the file descriptor for the connection, or negative
  22.  *  on errors.
  23.  */
  24. Export    int
  25. tcp_connect(const struct sockaddr_in    * remote,
  26.         const struct sockaddr_in    * local)
  27. {
  28.     int              s;
  29.  
  30.     s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  31.     if (s < 0)
  32.     return -1;
  33.     if (local  &&  bind(s, (struct sockaddr *)local, sizeof *local) < 0)
  34.     {    int saved_errno = errno;
  35.     close(s);
  36.     errno = saved_errno;
  37.     return -1;
  38.     }
  39.     if (connect(s, (struct sockaddr *)remote, sizeof *remote) < 0)
  40.     {    int saved_errno = errno;
  41.     close(s);
  42.     errno = saved_errno;
  43.     return -1;
  44.     }
  45.     return s;
  46. }
  47.